#!/usr/bin/env python
# -*- coding: utf-8 -*-
try:
    from HTMLParser import HTMLParser
    from urlparse import urljoin, urldefrag
except ImportError:
    from html.parser import HTMLParser
    from urllib.parse import urljoin, urldefrag
import tornado
from tornado import httpclient, gen, ioloop, queues
from tornado.httputil import HTTPHeaders
from tornado.curl_httpclient import CurlAsyncHTTPClient
import json
try:
    from urllib import urlencode  # py2
except ImportError:
    from urllib.parse import urlencode  # py3

async def fetch(url, method="GET", params={}, headers={}):
    """
    headers = {
        "Accept":"*/*",
        "Accept-Language":"en-US,en;q=0.8",
        "Host":urlparse(url).hostname,
        "Referer":referer,
        "Cookie":cookie,
        "Cache-Control":"no-cache",
        "Connection":"keep-alive",
        "User-Agent":'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36',
        }
    """
    http_client = CurlAsyncHTTPClient()
    http_client.configure("tornado.curl_httpclient.CurlAsyncHTTPClient")
    body = ""

    if (method != 'GET'):
        if headers.get('Content-Type') == 'application/x-www-form-urlencoded':
            body = urlencode(params)
        if headers.get('Content-Type') == 'application/json':
            body = json.dumps(params)
    else:
        body = None
    headers = HTTPHeaders(headers)
    req = httpclient.HTTPRequest(url, method=method, body=body, headers=headers, connect_timeout=10, request_timeout=30)
    response = None
    request_status = False
    try:
        response = await http_client.fetch(req)
    except:
        request_status = False
    # 对于网络异常进行一次重试
        try:
            response = await http_client.fetch(req)
        except:
            request_status = False

    return response


def send_request(url):
    io_loop = ioloop.IOLoop.current()
    # 使用lambda封装传递参数
    response = io_loop.run_sync(lambda: fetch(url, {}))
    return response

if __name__ == '__main__':
    pass
